using BlazorAdmin.Models;
using BootstrapBlazor.Components;
using System.ComponentModel;

namespace BlazorAdmin.Web.Shared.Models
{
    public class LogSearchModel : ITableSearchModel
    {
        [DisplayName("请求方")]
        public string? RequestUser { get; set; }

        [DisplayName("请求地址")]
        public string? RequestUrl { get; set; }

        [DisplayName("请求参数")]
        public string? RequestData { get; set; }

        [DisplayName("请求方法")]
        public string? RequestMethod { get; set; }

        [DisplayName("请求协议")]
        public string? RequestProtocol { get; set; }

        [DisplayName("业务描述")]
        public string? BizID { get; set; }

        [DisplayName("客户端IP")]
        public string? ClientIP { get; set; }

        [DisplayName("客户端浏览器")]
        public string? Browser { get; set; }

        [DisplayName("客户端系统")]
        public string? OS { get; set; }

        [DisplayName("用户代理")]
        public string? UserAgent { get; set; }

        [DisplayName("日志时间")]
        public DateTimeRangeValue? LogTime { get; set; }

        public LogSearchModel()
        {
            Reset();
        }

        public IEnumerable<IFilterAction> GetSearches()
        {
            var ret = new List<IFilterAction>();

            if (!string.IsNullOrEmpty(RequestUser))
                ret.Add(new SearchFilterAction(nameof(Log.RequestUser), RequestUser));

            if (!string.IsNullOrEmpty(RequestUrl))
                ret.Add(new SearchFilterAction(nameof(Log.RequestUrl), RequestUrl));

            if (!string.IsNullOrEmpty(RequestData))
                ret.Add(new SearchFilterAction(nameof(Log.RequestData), RequestData));

            if (!string.IsNullOrEmpty(RequestMethod))
                ret.Add(new SearchFilterAction(nameof(Log.RequestMethod), RequestMethod));

            if (!string.IsNullOrEmpty(RequestProtocol))
                ret.Add(new SearchFilterAction(nameof(Log.RequestProtocol), RequestProtocol));

            if (!string.IsNullOrEmpty(BizID))
                ret.Add(new SearchFilterAction(nameof(Log.BizID), BizID));

            if (!string.IsNullOrEmpty(ClientIP))
                ret.Add(new SearchFilterAction(nameof(Log.ClientIP), ClientIP));

            if (!string.IsNullOrEmpty(Browser))
                ret.Add(new SearchFilterAction(nameof(Log.Browser), Browser));

            if (!string.IsNullOrEmpty(OS))
                ret.Add(new SearchFilterAction(nameof(Log.OS), OS));

            if (!string.IsNullOrEmpty(UserAgent))
                ret.Add(new SearchFilterAction(nameof(Log.UserAgent), UserAgent));

            if (LogTime != null)
            {
                ret.Add(new SearchFilterAction(nameof(Log.LogTime), LogTime.Start, FilterAction.GreaterThanOrEqual));
                ret.Add(new SearchFilterAction(nameof(Log.LogTime), LogTime.End, FilterAction.LessThanOrEqual));
            }

            return ret;
        }

        public void Reset()
        {
            RequestUser = null;
            RequestUrl = null;
            RequestData = null;
            RequestMethod = null;
            RequestProtocol = null;
            BizID = null;
            ClientIP = null;
            Browser = null;
            OS = null;
            UserAgent = null;
            LogTime = null;
        }
    }
}
